<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true" draggable overflow>
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="120px" label-position="top">
            <el-form-item label="账号" prop="account">
                <el-input v-model="form.account" placeholder="请输入账号" clearable></el-input>
            </el-form-item>
            <el-form-item label="户名" prop="account_name">
                <el-input v-model="form.account_name" placeholder="请输入户名" clearable></el-input>
            </el-form-item>
            <el-form-item label="开户行" prop="account_bank">
                <el-input v-model="form.account_bank" placeholder="请输入开户行" clearable></el-input>
            </el-form-item>
            <el-form-item label="币种" prop="currency_id">
                <el-select v-model="form.currency_id" placeholder="请选择" filterable clearable>
                    <el-option v-for="item in currencyList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="地区" prop="area_id">
                <el-select v-model="form.area_id" placeholder="请选择" filterable clearable>
                    <el-option v-for="item in areaList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="结算方式" prop="paytype_id">
                <el-select v-model="form.paytype_id" placeholder="请选择" filterable clearable>
                    <el-option v-for="item in settleList" :key="item.id" :label="item.paytype_name" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="初始金额" prop="base_amount" v-if="mode == 'add'">
                <el-input v-model="form.base_amount" placeholder="初始金额" type="number" @mousewheel.prevent clearable></el-input>
            </el-form-item>
            <el-form-item label="金额" prop="balance" v-if="mode != 'add'">
                <el-input v-model="balance" placeholder="金额" readonly></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注" clearable type="textarea"></el-input>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>

export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增账户',
				edit: '编辑账户',
				show: '查看账户'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                account: "",
                account_name: "",
                account_bank: "",
                currency_id: "",
                area_id: "",
                paytype_id: "",
                base_amount: "",
                remark: "",
				system_disable: 0,
			},
            balance: "",
			//验证规则
			rules: {
				account: [{ required: true, message: '请输入账号' }],
				account_name: [{ required: true, message: '请输入户名' }],
			},
            areaList: [],
            settleList: [],
            currencyList: [],
		}
	},
	mounted() {
		this.getArea();
        this.getSettle();
        this.getCurrency();
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.$API.basic.account[this.form.id ? 'edit' : 'add'].post(this.form);
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
			this.form.id = data.id
			var res = await this.$API.basic.account.detail.get({ id: data.id });
            this.form.account = res.data.account
            this.form.account_name = res.data.account_name
            this.form.account_bank = res.data.account_bank
            this.form.area_id = res.data.area_id
            this.form.paytype_id = res.data.paytype_id
            this.form.base_amount = res.data.base_amount
            this.form.remark = res.data.remark
            this.form.system_disable = res.data.system_disable
            this.form.currency_id = res.data.currency_id
            this.balance = res.data.balance
		},
        // 地区列表
		async getArea() {
			var res = await this.$API.basic.area.all.get();
			this.areaList = res.data
		},
        // 结算方式列表
		async getSettle() {
			var res = await this.$API.basic.pay_type.all.get();
			this.settleList = res.data
		},
        // 币种列表
		async getCurrency() {
			var res = await this.$API.basic.currency.all.get();
			this.currencyList = res.data
		},
	}
}
</script>

<style></style>
